// Group Chat Connection Handelling Server

import java.io.*;
import java.net.*;
import java.lang.Thread;

/*
  Listen for incomming connection
  Spwan new Thread that would handel that connection
  Listen again for new connection
  Keep doing this for infinite time. 

*/

public class Server
{
 
  public static void main(String args[]) throws IOException
  {    

    String SERVER_HOST = new String("localhost");
    int SERVER_PORT = 0;

    if(args.length != 1)
    {
      System.out.println("Please enter the port Number on Command Line\nUsage java Server PORT_NO");
      System.exit(0);
    }else{
      SERVER_PORT = Integer.parseInt(args[0]);
      System.out.println("SERVER_PORT : " + SERVER_PORT);
    }
    
    ServerSocket myServerSocket;
    Socket connectionSocket = new Socket();
    myServerSocket = new ServerSocket(SERVER_PORT);

    while(true)
    {
      System.out.println("\nServer listening on port # : "+SERVER_PORT);
      connectionSocket = myServerSocket.accept();

      GroupChatServer myGroupChatServer = new GroupChatServer(connectionSocket);   
      Thread myThread = new Thread(myGroupChatServer);
      myThread.start();

    }//End while
  }//End main
}//End Server

